package com.atguigu.webadmin.controller.lease;


import com.atguigu.common.result.Result;
import com.atguigu.model.enums.AppointmentStatus;
import com.atguigu.model.pojo.ViewAppointment;
import com.atguigu.webadmin.service.ViewAppointmentService;
import com.atguigu.webadmin.vo.appointment.AppointmentQueryVo;
import com.atguigu.webadmin.vo.appointment.AppointmentVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "预约看房管理")
@RequestMapping("/admin/appointment")
@RestController
@CrossOrigin
@Slf4j
public class ViewAppointmentController {

    @Autowired
    private ViewAppointmentService viewAppointmentService;

    @Operation(summary = "分页查询预约信息")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "size",description = "每页需要展示的数量",in = ParameterIn.DEFAULT),
            @Parameter(name = "queryVo",description = "预约信息查询vo对象",in = ParameterIn.DEFAULT)
    })
    @GetMapping("page")
    public Result<IPage<AppointmentVo>> pageAppointmentByQuery(@RequestParam long current,
                                             @RequestParam long size,AppointmentQueryVo queryVo) {

        //创建分页对象，并传入当前页码和每页数量，将查询条件封装到iPage中
        IPage<AppointmentVo> iPage = new Page<>(current,size);
        //将封装好的查询条件iPage和查询条件queryVo传入service层方法，返回查询结果
        IPage<AppointmentVo> list = viewAppointmentService.pageAppointmentByQuery(iPage,queryVo);
        log.info("{}",list);
        return Result.ok(list);
    }

    @Operation(summary = "根据id更新预约状态")
    @Parameters({
            @Parameter(name = "id",description = "预约信息id",in = ParameterIn.DEFAULT),
            @Parameter(name = "status",description = "预约状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusById")
    public Result<Object> updateStatusById(@RequestParam Long id, @RequestParam AppointmentStatus status) {
        //创建Lambda更新包装对象
        LambdaUpdateWrapper<ViewAppointment> updateWrapper = new LambdaUpdateWrapper<>();
        //指定查询条件
        updateWrapper.eq(ViewAppointment::getId,id);
        //设置状态
        updateWrapper.set(ViewAppointment::getAppointmentStatus,status);
        //执行更新
        boolean update = viewAppointmentService.update(updateWrapper);
        return Result.ok(update);
    }

}
